var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var studentRouter = require("./routes/student/index")
var classRouter = require("./routes/classes/index");
//引用swagger
var swaggerJSDoc = require('swagger-jsdoc');
var app = express();

// swagger definition
var swaggerDefinition = {
  info: {
    title: '学生管理系统Api',
    version: '1.0.0',
    description: '学生系统api测试',
    host: 'localhost:3000',
    basePath: '/',
    contact: {
      name:'开发人员',
      url: 'http://localhost:3000',
      email: 'hotmial-f75@qq.com'
    }
 
}
 }

// options for the swagger docs
var options = {
  // import swaggerDefinitions
  swaggerDefinition: swaggerDefinition,
  //swagger会到该路径下去扫描所有js文件的注释来
  //生成api完档
  apis: ['./routes/**/*.js'],
};
// 初始化 swagger-jsdoc
var swaggerSpec = swaggerJSDoc(options);
app.get('/swagger.json', function(req, res) {
  res.setHeader('Content-Type', 'application/json');
  res.send(swaggerSpec);
});

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//做事
app.use("/student",studentRouter);
app.use("/classes",classRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
